package com.intro.action;

import javax.persistence.Entity;

import org.apache.struts2.ServletActionContext;

import com.intro.database.entity.News;
import com.intro.manager.INewsManager;
import com.intro.manager.impl.NewsManagerImpl;
import com.intro.util.Page;
import com.opensymphony.xwork2.ActionSupport;

@Entity
public class NewsAction extends ActionSupport {
	private int currentPage = 1;
	private int pageSize = 10;
	
	private int newsId;

	private INewsManager newsManager = new NewsManagerImpl();
	
	private String detailType;

	public String execute() throws Exception {

		return null;
	}

	public String fingAllByPage() throws Exception {
		Page page = newsManager.findAllNews(currentPage, pageSize);
		//把page放入请求域，在把他转换冲 json格式 在使用tmpl
		ServletActionContext.getRequest().setAttribute("page", page);
		return "showNews"; 
	}
	
	public String fingAllByProject() throws Exception {
		Page page = newsManager.findAllProject(currentPage, pageSize);
		//把page放入请求域，在把他转换冲 json格式 在使用tmpl
		ServletActionContext.getRequest().setAttribute("page", page);
		return "showProject"; 
	}
	
	public String newsDetail() throws Exception {
		News news = newsManager.findByNewsId(newsId);
		if(news==null) {
			return ERROR;
		}
		ServletActionContext.getRequest().setAttribute("news", news);
		
		if(detailType!=null&&detailType.equals("edit")) {
			if(news.getType()==news.TYPE_PROJECT) {
				return "showEditProcPage";
			} else {
				return "showEditPage";
			}
		}
		
		return "showNewsDetail";
	}
	
	public String newsDelete() throws Exception {
		News news = newsManager.findByNewsId(newsId);
		if(newsManager.deleteNews(newsId)==INewsManager.OPER_FAIL) {
			return ERROR;
		}
		if(news.getType()==News.TYPE_PROJECT) {
			return "showprocAction";
		} else {
			return "showNewsAction";
		}
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setNewsId(int newsId) {
		this.newsId = newsId;
	}

	public void setDetailType(String detailType) {
		this.detailType = detailType;
	}

	
	
	
	
	
}
